package com.ss.android.article.base.feature.message;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.SpipeCore;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.schema.AdsAppActivity;
import com.ss.android.article.base.feature.update.activity.UpdateDetailActivity;
import com.ss.android.article.base.feature.update.b.UpdateItemMgr;
import com.ss.android.article.base.feature.update.b.a.a$a;
import com.ss.android.article.base.ui.NotifyViewHelper;
import com.ss.android.article.browser.R;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.common.app.AbsFragment;
import com.ss.android.common.app.LifeCycleMonitor;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.UrlBuilder;
import com.ss.android.messagebus.SubscriptionRouter;
import com.ss.android.newmedia.a.AbstractFooter;
import com.ss.android.newmedia.util.AppUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class f extends AbsFragment implements com.ss.android.article.base.feature.message.d.b, a$a {
    final class f$a extends AbstractFooter {
        private final f j;

        public f$a(f arg1, View arg2) {
            super(arg2);
            this.j = arg1;
        }

        public final void onUserClick() {
            this.j.f();
        }
    }

    protected PullToRefreshListView a;
    protected TextView b;
    TextView c;
    AdsAppItem d;
    NotifyViewHelper e;
    protected Runnable f;
    Runnable g;
    private Context h;
    private List i;
    private n j;
    private boolean k;
    private Spipe l;
    private com.ss.android.article.base.feature.message.d m;
    private f$a n;
    private ListView o;
    private TextView p;
    private View q;
    private boolean r;
    private int s;
    private String t;
    private boolean u;
    private boolean v;
    private long w;

    public f() {

        this.i = new ArrayList();
        this.k = false;
        this.r = false;
        this.u = false;
        this.v = false;
        this.w = 0;
        this.f = new g(this);
        this.g = new l(this);
    }

    private void a(boolean arg4) {
        int v0 = View.GONE;
        if (this.isViewValid()) {
            if (arg4) {
                this.p.setVisibility(v0);
            } else {
                TextView v1 = this.p;
                if (this.i.isEmpty()) {
                    v0 = 0;
                }

                v1.setVisibility(v0);
            }
        }
    }

    private void a(boolean arg4, int arg5) {
        this.i.clear();
        this.i.addAll(new ArrayList(this.j.q));
        if (arg4) {
            this.m.notifyDataSetChanged();
            if (arg5 >= 0 && arg5 <= this.m.getCount()) {
                this.o.setSelection(arg5);
            }
        }
    }

    private boolean a(AdsAppItem arg10) {
        if (this.isViewValid()) {
            this.d = arg10;
            FragmentActivity v1 = this.getActivity();
            if (v1 != null && this.d != null && !android.support.design.a.isTextEmpty(this.d.displayInfo)) {
                String v0 = this.d.displayInfo;
                long v2 = this.d.displayDuration;
                this.c.setText(((CharSequence) v0));
                if (this.b != null) {
                    this.b.setVisibility(View.GONE);
                    this.b.removeCallbacks(this.f);
                }

                this.c.removeCallbacks(this.g);
                this.c.postDelayed(this.g, v2 * 1000);
                this.e.b(this.c);
                MobClickCombiner.onEvent(((Context) v1), "notify", "tips_show", this.d.id, 0);
                AppUtil.a(this.d.trackUrlList, this.getActivity());
            }
        }

        return true;
    }

    public final void a() {
        if (this.isViewValid()) {
            this.a(this.j.g);
        }

        if ((this.isViewValid()) && !this.isHidden() && (this.l.isLogin)) {
            this.j.a = this.l.userId;
            this.j.d();
        }
    }

    public final void a(int arg10, int arg11, m arg12) {
        int v4 = R.id.add;
        int v2 = R.id.z9;
        if (Logger.debug()) {
            Logger.d("UpdateMessage", "onUpdateMessageViewClick");
        }

        if (arg12 != null) {
            if (arg10 == v2 && !android.support.design.a.isTextEmpty(arg12.h)) {
                AppUtil.b(this.h, arg12.h);
                return;
            }

            if (!android.support.design.a.isTextEmpty(arg12.g)) {
                AppUtil.b(this.h, arg12.g);
                return;
            }

            if (arg11 != 4) {
                return;
            }

            if (arg10 != R.id.adb && arg10 != v2 && arg10 != v4) {
                return;
            }

            if (arg12.p == null) {
                return;
            }

            if (arg12.e > 0) {
                UrlBuilder v1 = new UrlBuilder("snssdk" + SpipeCore.getAppId() + "://thread_detail/");
                v1.addParam("tid", arg12.e);
                if (com.ss.android.article.base.feature.message.m.b.first.intValue() <= arg12.j && com.ss.android.article.base.feature.message.m.b.second.intValue() >= arg12.j) {
                    v1.addParam("user_id", arg12.o.userId);
                    v1.addParam("screen_name", arg12.o.screenName);
                    v1.addParam("dongtai_comment_id", arg12.f);
                    v1.addParam("show_comment_dialog", 1);
                    v1.addParam("refer", "click_message");
                }

                AdsAppActivity.openUrl(this.h, v1.build(), null);
            } else {
                String v8 = null;
                if (arg12.o != null) {
                    try {
                        v8 = arg12.o.toJson().toString();
                    } catch (Exception v0) {
                    }
                }
                UpdateDetailActivity.a(this.h, arg12.d, arg12.s, arg12.f, v8);
            }
            if (com.ss.android.article.base.feature.message.m.c.first.intValue() <= arg12.j && com.ss.android.article.base.feature.message.m.c.second.intValue() >= arg12.j) {
                if (arg12.j == 115) {
                    this.a("click_digg_reply");
                } else {
                    this.a("click_digg");
                }

                return;
            }

            if (com.ss.android.article.base.feature.message.m.b.first.intValue() <= arg12.j && com.ss.android.article.base.feature.message.m.b.second.intValue() >= arg12.j) {
                this.a("click_comment");
            }
        }
    }

    protected final void a(String arg3) {
        if (this.s == 4) {
            MobClickCombiner.onEvent(this.getActivity(), "message", arg3);
        } else if (this.s == 5) {
            MobClickCombiner.onEvent(this.getActivity(), "notification", arg3);
        }
    }

    public final void a(boolean arg7, int arg8, int arg9, AdsAppItem arg10) {
        int v0_1;
        if (this.isViewValid()) {
            boolean v0 = this.i.isEmpty();
            if (arg7) {
                this.a(true, -1);
            }

            this.w = System.currentTimeMillis();
            if (arg8 > 0) {
                this.b(this.getString(com.ss.android.article.browser.R.string.ss_error_unknown));
            } else {
                if (arg9 > 0) {
                    this.o.setSelection(0);
                    if (arg10 != null) {
                        this.a(arg10);
                    } else if (!v0) {
                        this.b(String.format(this.getString(com.ss.android.article.browser.R.string.ss_pattern_update), Integer.valueOf(arg9)));
                    }
                } else {
                    if (arg9 == 0 && !arg7) {
                        if (arg10 != null) {
                            this.a(arg10);
                        } else if (!v0) {
                            this.b(this.getString(com.ss.android.article.browser.R.string.ss_have_a_rest));
                        } else {
                        }
                    } else if (arg10 == null) {
                    } else {
                        this.a(arg10);
                    }
                }

                if (this.h == null) {
                } else if (!(this.h instanceof MessageTabActvity)) {
                } else if (((MessageTabActvity) this.h).isDestroyed()) {
                } else {
                    if (this.s == 5) {
                        v0_1 = 2;
                    } else if ("comment".equals(this.t)) {
                        v0_1 = 0;
                    } else {
                        v0_1 = 1;
                    }
                    SubscriptionRouter.c(new MessageTabActvity.a(v0_1));
                }
            }
            this.h();
        }
    }

    private void b(String arg5) {
        if ((this.isViewValid()) && this.b != null && arg5 != null) {
            if (this.c != null) {
                this.c.setVisibility(View.GONE);
                this.c.removeCallbacks(this.g);
            }

            this.b.removeCallbacks(this.f);
            this.b.setText(((CharSequence) arg5));
            this.b.postDelayed(this.f, 1500);
            this.e.b(this.b);
        }
    }

    public final void b() {
        if (this.isViewValid()) {
            this.h();
        }
    }

    protected final void c() {
        if (!this.j.g) {
            this.n.hideFooterContent();
            if (this.j.j) {
                if (System.currentTimeMillis() - this.w <= 1500) {
                    this.n.switchToMoreContent();
                } else {
                    FragmentActivity v0 = this.getActivity();
                    if (v0 != null) {
                        if (NetworkUtils.isWifi(((Context) v0))) {
                            this.f();
                        } else if (NetworkUtils.isNetworkAvailable(((Context) v0))) {
                            this.n.switchToMoreContent();
                        }
                    }
                }
            }
        }
    }

    protected final void d() {
        if (this.isViewValid()) {
            if (this.j.g) {
                if (this.a != null) {
                    this.a.onRefreshComplete();
                }

                this.b(this.getString(com.ss.android.article.browser.R.string.ss_hint_loading));
            } else {
                if (!NetworkUtils.isNetworkAvailable(this.getActivity())) {
                    if (this.a != null) {
                        this.a.onRefreshComplete();
                    }

                    this.b(this.getString(com.ss.android.article.browser.R.string.ss_error_no_connections));
                    return;
                }

                if (this.l.isLogin) {
                    this.j.a(true, 2);
                    return;
                }

                if (this.a != null) {
                    this.a.onRefreshComplete();
                }

                this.b(this.getString(com.ss.android.article.browser.R.string.ss_hint_not_login));
            }
        }
    }

    final void e() {
        if ((this.isViewValid()) && this.b != null) {
            this.e.a(this.b);
        }
    }

    protected final void f() {
        int v2 = 2;
        if (this.l.isLogin) {
            if (this.s == 4) {
                this.a("more_message");
            } else if (this.s == 5) {
                this.a("more_notify");
            }

            n v0 = this.j;
            if (((com.ss.android.article.base.feature.update.b.a) v0).q.isEmpty()) {
                ((com.ss.android.article.base.feature.update.b.a) v0).a(true, v2);
                return;
            }

            ((com.ss.android.article.base.feature.update.b.a) v0).a(false, v2);
        }
    }

    private void g() {
        if (!this.u) {
            n v0 = this.j;
            if (!((com.ss.android.article.base.feature.update.b.a) v0).g && (((com.ss.android.article.base.feature.update.b.a) v0).c.isLogin) && (NetworkUtils.isNetworkAvailable(((com.ss.android.article.base.feature.update.b.a) v0).e))) {
                ((com.ss.android.article.base.feature.update.b.a) v0).a(true, 2);
            }

            this.d();
        }

        this.u = true;
    }

    private void h() {
        boolean v0 = this.j.g;
        if (!v0) {
            if (this.a != null) {
                this.a.onRefreshComplete();
            }

            this.n.hideFooterContent();
        } else if (this.j.i) {
            if (this.a != null) {
                this.a.e();
            }

            this.n.hideFooterContent();
        } else {
            this.n.switchToLoading();
        }

        this.a(v0);
    }

    public final void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.l = Spipe.inst();
        AppData.getInstance();
        this.h = this.getActivity();
    }

    public final View onCreateView(LayoutInflater arg7, ViewGroup arg8, Bundle arg9) {
        int v5 = 4;
        int v0 = this.s == v5 ? R.layout.j9 : R.layout.update_notification_fragment;
        View v1 = arg7.inflate(v0, arg8, false);
        this.q = v1;
        this.b = v1.findViewById(com.ss.android.article.browser.R.id.notify_view);
        this.p = v1.findViewById(com.ss.android.article.browser.R.id.empty_view);
        this.a = v1.findViewById(com.ss.android.article.browser.R.id.listview);
        this.o = this.a.getRefreshableView();
        View v0_1 = arg7.inflate(com.ss.android.article.browser.R.layout.f0, this.o, false);
        v0_1.findViewById(com.ss.android.article.browser.R.id.ss_text);
        v0_1.findViewById(com.ss.android.article.browser.R.id.ss_more);
        this.n = new f$a(this, v0_1.findViewById(com.ss.android.article.browser.R.id.ss_footer_content));
        this.o.addFooterView(v0_1, null, false);
        this.e = new NotifyViewHelper(v1.getContext());
        this.o.addHeaderView(this.e.a());
        Bundle v0_2 = this.getArguments();
        if (v0_2 != null) {
            this.k = v0_2.getBoolean("from_mine");
            v0_2.getLong("user_id");
            this.s = v0_2.getInt("update_type");
            this.t = v0_2.getString("sub_msg_type");
        }

        this.j = this.s == v5 ? UpdateItemMgr.a(this.h, this.t) : UpdateItemMgr.b(this.h);
        this.j.b = this.k;
        return v1;
    }

    public final void onDestroy() {
        int v4 = 50;
        super.onDestroy();
        if (this.j != null) {
            n v1 = this.j;
            ((com.ss.android.article.base.feature.update.b.a) v1).r.remove(this);
            if ((((com.ss.android.article.base.feature.update.b.a) v1).r.weakHashMap.isEmpty()) && !((com.ss.android.article.base.feature.update.b.a) v1).g && ((com.ss.android.article.base.feature.update.b.a) v1).q.size() > v4) {
                ArrayList v0 = new ArrayList();
                ((List) v0).addAll(((com.ss.android.article.base.feature.update.b.a) v1).q.subList(0, v4));
                ((com.ss.android.article.base.feature.update.b.a) v1).q.clear();
                ((com.ss.android.article.base.feature.update.b.a) v1).q.addAll(((Collection) v0));
                ((com.ss.android.article.base.feature.update.b.a) v1).l = ((com.ss.android.article.base.feature.update.a.e) v1.q.get(49)).c;
                ((com.ss.android.article.base.feature.update.b.a) v1).j = true;
            }
        }

        if (this.e != null) {
            this.e.d();
        }
    }

    public final void onResume() {
        boolean v0 = false;
        super.onResume();
        if (this.v) {
            this.v = false;
            this.g();
        }

        if (this.r != AppData.isNightMode()) {
            if (!this.r) {
                v0 = true;
            }

            this.r = v0;
        } else {
            if (this.m == null) {
                return;
            }

            this.m.notifyDataSetChanged();
        }
    }

    public final void onViewCreated(View view, Bundle bundle) {
        super.onViewCreated(view, bundle);
        this.i.clear();
        this.a(false, -1);
        this.m = new com.ss.android.article.base.feature.message.d(this.h, this.i, this.s, this);
        if ((this.m instanceof AbsListView.RecyclerListener)) {
            this.o.setRecyclerListener(this.m);
        }

        if ((this.m instanceof LifeCycleMonitor)) {
            this.registerLifeCycleMonitor(this.m);
        }

        this.o.setAdapter(this.m);
        this.a.setOnRefreshListener(new com.ss.android.article.base.feature.message.h(this));
        this.a.setOnScrollListener(new i(this));
        this.a.setOnViewScrollListener(new com.ss.android.article.base.feature.message.j(this));
        this.a(true, 0);
        this.h();
        this.j.r.add(this);
        this.r = false;
        this.c = this.q.findViewById(com.ss.android.article.browser.R.id.notify_recommand_view);
        this.c.setOnClickListener(new k(this));
        if (this.s == 4) {
            this.p.setText(this.getResources().getString(com.ss.android.article.browser.R.string.update_msg_empty));
        } else {
            this.p.setText(this.getResources().getString(com.ss.android.article.browser.R.string.update_notification_empty));
        }
    }

    public final void setUserVisibleHint(boolean userVisibleHint) {
        super.setUserVisibleHint(userVisibleHint);
        if (userVisibleHint) {
            if (this.isActive()) {
                this.g();
            } else {
                this.v = true;
            }
        }
    }
}

